/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TbedTreeViewPanel.java
 *
 * Created on 28.Ara.2009, 14:10:30
 */
package org.testis.gui.mvc.view.labels;

import org.testis.gui.mvc.controller.TestDesignControllerList;
import org.testis.gui.mvc.controller.testdesign.LabelSettingsController;
import org.testis.gui.mvc.view.AbstractViewPanel;
import org.testis.utils.tree.DefaultMutableLabelNode;
import java.beans.PropertyChangeEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author selcuk
 */
public class LabelSettingsPanel extends javax.swing.JPanel implements AbstractViewPanel {

    private LabelSettingsController m_controller = null;
    private DefaultMutableLabelNode m_treeRoot = null;

    /** Creates new form TbedTreeViewPanel */
    public LabelSettingsPanel() {
        m_controller = TestDesignControllerList.newInstance().getLabelSettingsController();
        m_controller.addView(this);
        m_treeRoot = m_controller.getLabelTreeRoot();
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        labelTree = new DefaultLabelJTree();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));

        labelTree.setModel(new DefaultTreeModel(null));
        labelTree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                labelTreeMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(labelTree);

        add(jScrollPane1);
    }// </editor-fold>//GEN-END:initComponents

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(
                LabelSettingsController.TREE_LOAD_PROPERTY)) {
            DefaultMutableLabelNode root = (DefaultMutableLabelNode) evt.getNewValue();
            m_treeRoot = root;
            loadTree(root);
        } else if (evt.getPropertyName().equals(
                LabelSettingsController.TREE_REFRESH_PROPERTY)) {
            labelTree.updateUI();
        } else if (evt.getPropertyName().equals(
                LabelSettingsController.SELECTION_TREE_PATH_PROPERTY)) {
            TreePath path = (TreePath) evt.getNewValue();
            labelTree.setSelectionPath(path);
        } else if (evt.getPropertyName().equals(
                LabelSettingsController.ERROR_PROPERTY)) {
            String val = (String) evt.getNewValue();
            JOptionPane.showMessageDialog(
                    this,
                    val,
                    "Error Dialog",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    private void loadTree(DefaultMutableLabelNode root) {
        ((DefaultLabelJTree) labelTree).loadLabels(root);
    }

    private void labelTreeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_labelTreeMouseClicked
        TreePath path = labelTree.getPathForLocation(evt.getX(),
                evt.getY());
        labelTree.setSelectionPath(path);
        DefaultMutableLabelNode selectedNode = ((DefaultLabelJTree) labelTree).getSelectedNode();
        if (selectedNode != null) {
            m_controller.operationSelectLabel(selectedNode);
        }
    }//GEN-LAST:event_labelTreeMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree labelTree;
    // End of variables declaration//GEN-END:variables

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("Label Settings");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().add(new LabelSettingsPanel());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
